package org.example.demo;

import org.reactivestreams.Subscriber;
import org.reactivestreams.Subscription;

import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class ReactiveTest {


    public static void main(String[] args) {
        Set<Integer> elements = new HashSet<>();
        for (int i = 0; i < 10; i++) {
            elements.add(i);
        }

        final ExecutorService executorService = Executors.newFixedThreadPool(5);

        AsyncIterablePublisher<Integer> publisher = new AsyncIterablePublisher<>(elements, executorService);

//        publisher.subscribe(new Subscriber<Integer>() {
//            @Override
//            public void onSubscribe(Subscription subscription) {
//                System.out.println("---------------------");
//                System.out.println("开始订阅...........");
//                System.out.println(subscription);
//                System.out.println("---------------------");
//            }
//
//            @Override
//            public void onNext(Integer integer) {
//                System.out.println("---------------------");
//                System.out.println("开始获取下一个...........");
//                System.out.println("----+"+integer+"----");
//                System.out.println("---------------------");
//            }
//
//            @Override
//            public void onError(Throwable throwable) {
//                System.out.println("---------------------");
//                System.out.println("开始获取错误...........");
//                System.out.println("----+"+throwable.getCause()+"----");
//                System.out.println("---------------------");
//            }
//
//            @Override
//            public void onComplete() {
//                System.out.println("---------------------");
//                System.out.println("开始获取完成...........");
//                System.out.println("---------------------");
//            }
//        });

        final AsyncSubscriber<Integer> subscriber = new AsyncSubscriber<Integer>
                (Executors.newFixedThreadPool(2)) {
            @Override
            protected boolean whenNext(Integer element) {
                System.out.println("接收到的流元素：" + element);
                return true;
            }
        };
        publisher.subscribe(subscriber);
    }


}

